package com.dc.project.gen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dc.common.exception.ServiceException;
import com.dc.common.utils.DateUtil;
import com.dc.project.gen.dao.GenCodeDao;
import com.dc.project.gen.entity.GenCode;
import com.dc.project.gen.service.GenCodeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 编码规程生成表(GenCode)表服务实现类
 *
 * @author zhuangcy
 * @since 2021-06-19 10:12:53
 */
@Service("genCodeService")
public class GenCodeServiceImpl extends ServiceImpl<GenCodeDao, GenCode> implements GenCodeService {

    /**
     * 根据表名获取编码序号
     *
     * @param tableName
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    @Override
    public Integer getOneCode(String tableName) {
        QueryWrapper<GenCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(GenCode::getTableName, tableName)
                .isNull(GenCode::getGenDate);
        GenCode one = this.getOne(queryWrapper);
        Integer resCode;
        if (null == one) {
            one = new GenCode();
            one.setTableName(tableName);
            one.setCode(2);
            if (this.save(one)) {
                return 1;
            } else {
                throw new ServiceException("编码生成失败");
            }
        } else {
            resCode = one.getCode();
            int code = one.getCode() + 1;
            one.setCode(code);
            if (this.updateById(one)) {
                return resCode;
            } else {
                throw new ServiceException("编码生成失败");
            }
        }
    }

    @Override
    public Integer getOneDateCode(String tableName) {
        QueryWrapper<GenCode> queryWrapper = new QueryWrapper<>();
        Date nowDate = DateUtil.getNowDate();
        queryWrapper.lambda()
                .eq(GenCode::getTableName, tableName)
                .apply("DATE_FORMAT(gen_date,'%Y-%m-%d')=DATE_FORMAT({0},'%Y-%m-%d')", nowDate);
        GenCode one = this.getOne(queryWrapper);
        Integer resCode;
        if (null == one) {
            one = new GenCode();
            one.setTableName(tableName);
            one.setGenDate(nowDate);
            one.setCode(2);
            if (this.save(one)) {
                return 1;
            } else {
                throw new ServiceException("编码生成失败");
            }
        } else {
            if (DateUtil.dateTime(nowDate).equals(DateUtil.dateTime(one.getGenDate()))) {
                resCode = one.getCode();
                int code = one.getCode() + 1;
                one.setCode(code);
            } else {
                resCode = 1;
                one.setCode(2);
            }
            one.setGenDate(nowDate);
            if (this.updateById(one)) {
                return resCode;
            } else {
                throw new ServiceException("编码生成失败");
            }
        }
    }
}